<?php

namespace app\home\model;

use app\common\loader\ModelMerchant;
use app\common\model\virtual\VirtualPromoProductModel as VirtualPromoProduct;
use app\home\loader\Model;
use think\Exception;

/**
 * 虚码活动商品
 */
class VirtualPromoProductModel extends VirtualPromoProduct
{


    /**
     * 获取活动商品信息
     * @param string $mer_id |商户id
     * @param int    $vp_id | 活动id
     * @param string $product_code |商品code
     * @param int    $type |1：单个商品， 2：多个商品
     * @throws Exception
     */
    public function getActivityProduct(string $mer_id, int $vp_id, string $product_code, int $type = 1)
    {

        $where = ['mer_id' => $mer_id, 'vp_id' => $vp_id];

        if ($type === 1) {
            $where['product_code'] = $product_code;
        }
        $activity_product = $this->db()->where($where)->field('product_code,num')->select();
        if (empty($activity_product)) {
            throw  new Exception('本商品未参加该活动,请联系经销商.', API_ERROR);
        }

        $product_title = ModelMerchant::Product()->getProductTitle($mer_id, array_column($activity_product, 'product_code'));

        foreach ($activity_product as &$row) {
            $row['scan'] = $row['product_code'] === $product_code ? 1 : 0;
            $row['product_title'] = $product_title[$row['product_code']] ?? '未知商品';
        }
        return $activity_product;
    }


    /**
     * 根据活动获取商品列表
     * @param string $mer_id | 商户id
     * @param array  $activity_list | 活动列表
     * @param int    $user_id | 用户id
     * @param string $product_code | 查询信息
     * @return array
     * @throws Exception
     */
    public function getActivityProductList(string $mer_id, array $activity_list, int $user_id, string $product_code = ''): array
    {
        if (!is_array($activity_list) || empty($activity_list)) {
            return [];
        }

        $product = $this->db()->where(['mer_id' => $mer_id])->whereIn('vp_id', array_column($activity_list, 'id'))
            ->field(['id', 'num', 'product_code', 'vp_id'])
            ->select();

        $activity_product_title = ModelMerchant::Product()->getProductTitle($mer_id, array_column($product, 'product_code'));

        $product_list = [];
        foreach ($product as $v) {
            $product_list[$v['vp_id']][$v['product_code']] = [
                'product_code' => $v['product_code'],
                'product_title' => $activity_product_title[$v['product_code']] ?? '其他商品',
                'num' => $v['num']
            ];
        }

        $result = [];
        foreach ($activity_list as $key => $v) {
            //  单商品,只获取当前已扫码商品的,联合商品,返回全部
            if ($v['product_type'] === 1) {
                $virtuaPromo = Model::VirtualPromo()->getValidPromInfo((int)$v['id'], (int)$user_id, $v);
                $small_ticket = $virtuaPromo['small_ticket'] ?? 0;
                $list[] = $product_list[$v['id']][$product_code] ?? [];
            } else {
                $small_ticket = $v['option']['small_ticket'] ?? 0;
                $list = array_values($product_list[$v['id']]);
            }
            $result[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'product_type' => $v['product_type'],
                'small_ticket' => $small_ticket,
                'list' => $list
            ];
        }

        return $result;
    }
}
